package com.heima.article.listener;

import com.heima.article.service.ApArticleService;
import com.heima.common.constants.message.WmNewsUpDownConstants;
import com.heima.common.util.JsonUtils;
import com.heima.model.media.dtos.WmNewsDownOrUpDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class WmNewsDownOrUpListener {

    @Autowired
    private ApArticleService articleService;

    @KafkaListener(topics = WmNewsUpDownConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void onMessage(ConsumerRecord<String, String> consumerRecord){
        if(consumerRecord == null){
            return;
        }
        String json = consumerRecord.value();
        log.info("收到自媒体文章上下架消息：{}", json);
        articleService.downOrUp(JsonUtils.toBean(json, WmNewsDownOrUpDTO.class));
    }


}
